export default {
    namespaced: true,
    state: {
        tag: "购物车模块cart",
        carts: []
    },
    mutations: {
        SET_CART(state, payload) {
            let id = payload.id;
            let target = state.carts.find(item => item.id == id);
            if (!target) {
                let obj = {
                    id,
                    title: payload.title,
                    price: payload.price,
                    count: 1
                }
                state.carts.push(obj)
            } else {
                target.count++
            }
        },
    },
    actions: {
        GET_PRODUCT(context, payload) {
            context.commit('SET_CART', payload)
            context.commit('Product/DECREMENT_INVENTORY', payload.id, {
                root: true
            })
        }
    },
    getters: {
        total(state) {
            return state.carts.reduce((total, item) => {
                return total += item.count * item.price
            }, 0).toFixed(2);
        }
    },
}